package cn.gcjxsc.repository.specs;

import cn.gcjxsc.consts.WithdrawStatus;
import cn.gcjxsc.consts.WithdrawType;
import cn.gcjxsc.data.query.common.WithdrawQuery;
import cn.gcjxsc.entity.account.Withdraw;
import cn.gcjxsc.entity.account.Withdraw_;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pumbaa
 * @since 1.0
 */
public class WithdrawSpec {
    /**
     * 构建提现查询条件
     * @param filter 过滤条件
     * @return
     */
    public static Specification<Withdraw> buildSpec(final WithdrawQuery filter){
        return new Specification<Withdraw>() {
            @Override
            public Predicate toPredicate(Root<Withdraw> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return buildFilter(filter, root, cb);
            }
        };
    }

    public static Predicate buildFilter(WithdrawQuery filter, Root<Withdraw> root, CriteriaBuilder builder) {
        List<Predicate> predicateList = new ArrayList<>();

        if (!StringUtils.isEmpty(filter.getSn())) {
            if (filter.getSnType() == 1) {
                predicateList.add(builder.equal(root.get(Withdraw_.sn), filter.getSn()));
            }
            else {
                predicateList.add(builder.like(root.get(Withdraw_.sn), "%" + filter.getSn() + "%"));
            }
        }

        //开始时间
        if (filter.getStartTime() != null && filter.getStartTime() > 0) {
            if (filter.getDateType() == WithdrawQuery.DATE_TYPE_CHECK) {
                predicateList.add(builder.greaterThanOrEqualTo(root.get(Withdraw_.checkTime), filter.getStartTime()));
            }
            else if (filter.getDateType() == WithdrawQuery.DATE_TYPE_DONE) {
                predicateList.add(builder.greaterThanOrEqualTo(root.get(Withdraw_.doneTime), filter.getStartTime()));
            }
            else {
                predicateList.add(builder.greaterThanOrEqualTo(root.get(Withdraw_.created), filter.getStartTime()));
            }
        }
        //结束时间
        if (filter.getEndTime() != null && filter.getEndTime() > 0) {
            if (filter.getDateType() == WithdrawQuery.DATE_TYPE_CHECK) {
                predicateList.add(builder.lt(root.get(Withdraw_.checkTime), filter.getEndTime()));
            }
            else if (filter.getDateType() == WithdrawQuery.DATE_TYPE_DONE) {
                predicateList.add(builder.lt(root.get(Withdraw_.doneTime), filter.getEndTime()));
            }
            else {
                predicateList.add(builder.lt(root.get(Withdraw_.created), filter.getEndTime()));
            }
        }
        if (filter.getStoreId() != null && filter.getStoreId() > 0) {
            predicateList.add(builder.equal(root.get(Withdraw_.storeId), filter.getStoreId()));
        }
        if (filter.getExtraId() != null && filter.getExtraId() > 0) {
            predicateList.add(builder.equal(root.get(Withdraw_.extraId), filter.getExtraId()));
        }

        if (filter.getStatus() != null && filter.getStatus() > -1) {
            predicateList.add(builder.equal(root.get(Withdraw_.withdrawStatus), WithdrawStatus.fromCode(filter.getStatus())));
        }

        //指定人员
        if (filter.getType() != null) {
            predicateList.add(builder.equal(root.get(Withdraw_.withdrawType), WithdrawType.fromCode(filter.getType())));
            if (filter.getOwnerId() != null && filter.getOwnerId() > 0) {
                predicateList.add(builder.equal(root.get(Withdraw_.ownerId),filter.getOwnerId()));
            }
        }

        Predicate[] predicates = new Predicate[predicateList.size()];
        return builder.and(predicateList.toArray(predicates));
    }
}
